/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import java.sql.Statement;
import model.Personne;

/**
 *
 * @author Paul
 */
public class DAOPersonne {

private String daoLogin;
private String daoPassword;
  
public Personne rechercheUtilisateur(String login, String mdp) throws SQLException {
       
            /*
             * Base de données
             */
            PreparedStatement st = null;
            Personne p=null;
            try {
                Connection conn = DBConnection.getConnection();
                String query = "select * from personne where login='"+login+"' and password='"+mdp+"';";
                st = conn.prepareStatement(query);
                ResultSet res = st.executeQuery(query);
                if(res.next()){
                    String log = res.getString("login");
                    String password = res.getString("password");
                   String nom= res.getString("nom");
                   String prenom=res.getString("prenom");
                   int idp=0;
                   String type=null;
                   Date d =null;
                    p = new Personne(idp,nom,prenom,d,type,log,password);
                }
                }catch(SQLException e){
                    e.printStackTrace();
            }
           
            return p;
             } 

public Personne rechercheUtilisateur(Integer idPersonne) throws SQLException {
       
            /*
             * Base de données
             */
            PreparedStatement st = null;
            Personne p=null;
            try {
                Connection conn = DBConnection.getConnection();
                String query = "select * from personne where id_personne = "+idPersonne+";";
                st = conn.prepareStatement(query);
                ResultSet res = st.executeQuery(query);
                if(res.next()){
                    String log = res.getString("login");
                    String password = res.getString("password");
                   String nom= res.getString("nom");
                   String prenom=res.getString("prenom");
                   int idp= idPersonne;
                   Date dateNaissance = res.getDate("dateNaissance");
                   String type=res.getString("typeUtilisateur");
                   p = new Personne(idp,nom,prenom,dateNaissance,type,log,password);
                }
                }catch(SQLException e){
                    e.printStackTrace();
            }
           
            return p;
             }

    public boolean update (Personne obj) throws SQLException {
        
         PreparedStatement ps = null;
         Personne p=null;
         boolean del = false;
         int nb = 0;         
         p = rechercheUtilisateur(obj.getId());
         String query = "UPDATE `personne` SET `nom`=?,`prenom`=?,`dateNaissance`=?,`typeUtilisateur`= ?,`login`=?,`password`=? WHERE `id_personne` = ?";
            try {
            Connection conn = DBConnection.getConnection();//Creation de la connexion
            ps = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); //preparation de la requete
            ps.setString(1,p.getNom());
            ps.setString(2, p.getPrenom());
            ps.setDate(3, p.getDatenaissance());
            ps.setString(4, p.getTypeUtilisateur());
            ps.setString(5, p.getLogin());
            ps.setString(6, p.getMdp());
            ps.setObject(7,p.getId());
            nb = ps.executeUpdate();
                
                }catch(SQLException e){
                    e.printStackTrace();
            }
            return del;
    }

    public boolean checkLogin(String login) {
        return (!daoLogin.equals(login));
    }
    
    public boolean checkPassword(String password) {
        return (!daoPassword.equals(password));
    }
    
    public boolean isNotEmptyLogin(String login) {
        return (!"".equals(login));
    }

    public boolean isNotEmptyPassword(String password) {
        return (!"".equals(password));
    }
    
}
